﻿/************************************************************************

 This file is part of EscapeFromPrisonPlanet.

 EscapeFromPrisonPlanet is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 EscapeFromPrisonPlanet is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2012 Scott Bevin. all rights reserved

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LudumDare25Project.GameScreens
{
    internal class PauseScreen : GameScreen
    {
        private SpriteFont font;

        private WhiteFadeScreen bgWhiteFade = null;

        private WhiteFadeScreen whiteFadeOut = null;
        private GameScreen gamescreenToTrigger = null;

        private Menu menu;

        private GameplayScreen GameplayScr;

        public PauseScreen(GameplayScreen gamescreen)
            : base(true)
        {
            GameplayScr = gamescreen;
        }

        public override void OnAdded()
        {
            bgWhiteFade = new WhiteFadeScreen();
            GameScreenManager.AddGameScreen(bgWhiteFade, this, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef);

            font = ContentManager.Load<SpriteFont>("Fonts/Linds48");

            menu = new Menu(ContentManager.Load<SpriteFont>("Fonts/Linds24"), ContentManager);

            menu.AddEntry("Resume", this.Resume);
            menu.AddEntry("Restart", this.Rety);
            menu.AddEntry("Quit To Main Menu", this.MainMenu);
            menu.AddEntry("Quit Game", this.QuitGame);

            base.OnAdded();
        }

        public override void OnRemoved()
        {
            bgWhiteFade.FadeOut();

            base.OnRemoved();
        }

        public override void Update(float dt, InputManager input)
        {
            menu.Update(dt, input, Game.GraphicsDevice);

            /*if (whiteFadeOut == null)
            {
                if (input.MouseButtonStates[(int)InputManager.MouseButtons.LeftButton].Released ||
                    input.MouseButtonStates[(int)InputManager.MouseButtons.RightButton].Released)
                {
                    whiteFadeOut = new WhiteFadeScreen();
                    gamescreenToTrigger = new MainMenuScreen();

                    GameScreenManager.AddGameScreen(whiteFadeOut, null, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
                }
            }*/

            if (whiteFadeOut != null && whiteFadeOut.State == WhiteFadeScreen.States.FadedIn)
            {
                whiteFadeOut.FadeOut();
                GameScreenManager.AddGameScreen(gamescreenToTrigger, whiteFadeOut, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef);
                this.KillScreen = true;
            }

            base.Update(dt, input);
        }

        public override void Render(float dt, GraphicsDevice gd, SpriteBatch sprites)
        {
            sprites.Begin();

            string text = String.Format("Game Paused!");

            sprites.DrawString(font, text, new Vector2(500.0f, 200.0f), Color.Black, 0.0f,
                font.MeasureString(text) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);

            sprites.End();

            menu.Render(gd, sprites);

            base.Render(dt, gd, sprites);
        }

        private void Resume(Menu.MenuEntry entry)
        {
            this.KillScreen = true;
        }

        private void Rety(Menu.MenuEntry entry)
        {
            GameplayScr.KillScreen = true;

            whiteFadeOut = new WhiteFadeScreen();
            gamescreenToTrigger = new GameplayScreen();

            GameScreenManager.AddGameScreen(whiteFadeOut, null, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
        }

        private void MainMenu(Menu.MenuEntry entry)
        {
            GameplayScr.KillScreen = true;

            whiteFadeOut = new WhiteFadeScreen();
            gamescreenToTrigger = new MainMenuScreen();

            GameScreenManager.AddGameScreen(whiteFadeOut, null, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
        }

        private void QuitGame(Menu.MenuEntry entry)
        {
            this.Game.Exit();
        }
    }
}